package com.db.demo;

import java.util.HashMap;
import java.util.Random;

import com.db.collections.IntegerHashMap;
import com.db.collections.IntegerHashMapImpl;

public class IntegerHashMapDemo {
	private static Runtime r = Runtime.getRuntime();

	public static void main(String[] args) {
		Random rand = new Random();
		long memBefore, memDelta;

		System.out.println("Creating HashMap<Integer, Integer>");
		memBefore = usedMemory();

		HashMap<Integer, Integer> checkMap = new HashMap<>();
		for (int i = 0; i < 1_000_000; i++) {
			int k = rand.nextInt();
			int v = rand.nextInt();
			checkMap.put(k, v);
		}

		memDelta = usedMemory() - memBefore;
		System.out.printf("Consumed memory: %,d%n", memDelta);

		//

		System.out.println("Creating IntegerHashMap");
		memBefore = usedMemory();

		IntegerHashMap map = new IntegerHashMapImpl();
		for (Integer key : checkMap.keySet()) {
			map.put(key, checkMap.get(key));
		}

		memDelta = usedMemory() - memBefore;
		System.out.printf("Consumed memory: %,d%n", memDelta);

		// prevent app from shutting down (to use jvisualvm)
//		while (true);
	}

	private static long usedMemory() {
		return r.totalMemory() - r.freeMemory();
	}
}
